<template>
    <j-modal
            :title="title"
            :width="width"
            :visible="visible"
            :switchFullscreen="true"
            @ok="handleOk"
            @cancel="handleCancel"
    >
        <h2 class="text-center">【{{customerName}}】医疗废物管理台账</h2>
        <div style="text-align: right;margin-bottom:10px;" >
            <a-button type="primary" icon="printer" v-print="'#printTableList'">打印</a-button>
            <a-button type="primary" icon="download" @click="openExcel" style="margin-left: 10px;">
                导出Excel
            </a-button>
        </div>
        <a-table
                ref="table"
                size="middle"
                rowKey="id"
                :columns="columns"
                :data-source="dataSource"
                bordered
                :pagination="false"
                :loading="loading"
                class="j-table-force-nowrap"
        >
            <template slot="imgSlot" slot-scope="text">
                <span v-if="!text" style="font-size: 12px;font-style: italic;"></span>
                <img v-else :src="getImgView(text)" height="25px" alt=""
                     style="width: auto;height: 50px;"/>
            </template>
        </a-table>

        <div v-show="false">
            <div id="printTableList">
                <h2 style="text-align: center;font-weight: bold;">机构信息列表</h2>
                <a-table
                        size="small"
                        bordered
                        rowKey="id"
                        :columns="columns"
                        :dataSource="dataSource"
                        :pagination="false"
                >
                    <template slot="htmlSlot" slot-scope="text">
                        <div style="color: black !important;" v-html="text"></div>
                    </template>
                    <template slot="imgSlot" slot-scope="text">
                        <span v-if="!text" style="font-size: 12px;font-style: italic;"></span>
                        <img v-else :src="getImgView(text)" height="25px" alt=""
                             style="width: auto;height: 50px;"/>
                    </template>
                </a-table>
            </div>
        </div>

    </j-modal>
</template>

<script>
  import * as customer from '@/api/customer'
  import moment from 'moment'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { mixinDevice } from '@/utils/mixin'
  import { downloadFile, getAction, putAction } from '@/api/manage'

  export default {
    mixins: [JeecgListMixin, mixinDevice],
    data() {
      return {
        title: '医疗废物台账列表',
        width: '80%',
        visible: false,
        disableSubmit: false,
        dataSource : [],
        columns:[],
        customerName:'',
        loading: false,
        url: {
          exportXlsUrlOkForImages: '/hc/hcRecycleBagcode/exportInspection'
        },
        // 导出查询参数
        queryParam: {}
      }
    },
    methods: {
      async show(cus,customerNumber,customerName) {
        this.visible = true;
        this.customerName = customerName;
        if (cus) {
          let params = {
            creatTimeBegin : cus.time,
            creatTimeEnd : cus.time,
            customerNumber: customerNumber // 机构编码
          };
          this.queryParam = params;
          let res = await customer.getEveryDayStatistic(params);
          if (res) {
            this.dataSource = res.list || []
            this.dataSource.push({
              colSpan: 3,
              infectiousWeight: res['sumInfectiousWeight'] || 0,
              infectiousBagNum: res['infectiousBagSum'] || 0,
              traumaticWeight: res['sumTraumaticWeight'] || 0,
              traumaticBagNum: res['traumaticBagSum'] || 0,
              pathologicalWeight: res['sumPathologicalWeight'] || 0,
              pathologicalBagNum: res['pathologicalBagSum'] || 0,
              medicatedWeight: res['sumMedicatedWeight'] || 0,
              medicatedBagNum: res['medicatedBagSum'] || 0,
              chemicalWeight: res['sumChemicalWeight'] || 0,
              chemicalBagNum: res['chemicalBagSum'] || 0,
            })
          }
        }
        await this.setColumns()

      },

      // 设置表格头部信息
      setColumns() {
        this.columns = [
          {
            title: '序号',
            dataIndex: 'id',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            scopedSlots: { customRender: 'htmlSlot' },
            customRender: (text, row, index) => {
              const obj = {
                children: parseInt(index) + 1,
                attrs: {}
              }
              if (row.colSpan) {
                obj.children = <div style="color:red;font-weight:600;">合计</div>
                obj.attrs.colSpan = row.colSpan
              }
              return obj
            }
          },
          {
            title: `日期`,
            dataIndex: 'createTime',
            key: `createTime`,
            width: 100,
            align: 'center',
            customRender: (value, row, index) => {
              const formatterTime = (val) =>{
                return val ? moment(val).format('yyyy-MM-DD') : ''
              };
              const obj = {
                children:  formatterTime(value),
                attrs: {}
              }
              if (row.colSpan) {
                obj.attrs.colSpan = 0
              }
              return obj
            }
          },
          {
            title: '部门/科室',
            align: 'center',
            dataIndex: 'customerDepartmentName',
            key: 'customerDepartmentName',
            customRender: (value, row, index) => {
              const obj = {
                children: value,
                attrs: {}
              }
              if (row.colSpan) {
                obj.attrs.colSpan = 0
              }
              return obj
            }
          },
          {
            title: '医疗废物',
            dataIndex: 'id',
            key: `yiliaofeiwu`,
            children: [
              {
                title: `感染性`,
                dataIndex: 'infectiousWeight',
                key: `infectiousWeight`,
                children: [
                  {
                    title: `袋数`,
                    dataIndex: 'infectiousBagNum',
                    key: `infectiousBagNum`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0}</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                  {
                    title: `重量(kg)`,
                    dataIndex: 'infectiousWeight',
                    key: `infectiousWeight`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0} kg</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                ]
              },
              {
                title: `损伤性`,
                dataIndex: 'traumaticWeight',
                key: `traumaticWeight`,
                children: [
                  {
                    title: `袋数`,
                    dataIndex: 'traumaticBagNum',
                    key: `traumaticBagNum`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0}</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                  {
                    title: `重量(kg)`,
                    dataIndex: 'traumaticWeight',
                    key: `traumaticWeight`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0} kg</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                ]
              },
              {
                title: `病理性`,
                dataIndex: 'pathologicalWeight',
                key: `pathologicalWeight`,
                children: [
                  {
                    title: `袋数`,
                    dataIndex: 'pathologicalBagNum',
                    key: `pathologicalBagNum`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0}</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                  {
                    title: `重量(kg)`,
                    dataIndex: 'pathologicalWeight',
                    key: `pathologicalWeight`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0} kg</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                ]
              },
              {
                title: `药物性`,
                dataIndex: 'medicatedWeight',
                key: `medicatedWeight`,
                children: [
                  {
                    title: `袋数`,
                    dataIndex: 'medicatedBagNum',
                    key: `medicatedBagNum`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0}</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                  {
                    title: `重量(kg)`,
                    dataIndex: 'medicatedWeight',
                    key: `medicatedWeight`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0} kg</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                ]
              },
              {
                title: `化学性`,
                dataIndex: 'chemicalWeight',
                key: `chemicalWeight`,
                children: [
                  {
                    title: `袋数`,
                    dataIndex: 'chemicalBagNum',
                    key: `chemicalBagNum`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0}</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                  {
                    title: `重量(kg)`,
                    dataIndex: 'chemicalWeight',
                    key: `chemicalWeight`,
                    customRender: (value, row, index) => {
                      const obj = {
                        children: value || '',
                        attrs: {}
                      }
                      if (row.colSpan) {
                        obj.children =
                      <div style="color:red;font-weight:600;">{value || 0} kg</div>
                        obj.attrs.colSpan = 1
                      }
                      return obj
                    }
                  },
                ]
              }
            ]
          },
          {
            title: `部门/科室签字`,
            dataIndex: 'customerDepartmentUrl',
            key: `customerDepartmentUrl`,
            width: 100,
            align: 'center',
            scopedSlots: { customRender: 'imgSlot' }
          }
        ];
      },

      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        this.close()
      },
      handleCancel() {
        this.close()
      },

      // 导出数据
      openExcel() {
        let param = { ...this.queryParam }
        let name = `台账数据统计-${new Date().FormatDate()}.xls`
        downloadFile(this.url.exportXlsUrlOkForImages, name, param)
      }
    }
  }
</script>
